<?php
session_start();
require_once '../header.php';
require_once 'array.php';
function __autoload($className)
	{
    	require_once('../class/' . $className . '.class.php');
	}
session::need_member();
$Plan=$_SESSION["plan"];
$UserId=session::getuserid();
//$CanBuy=user::need_CanBuy($UserId);
$CanBuy='true';
if($CanBuy==FALSE)
{
	$_SESSION["erro"]="Your deposit is not enough 40$, now you need to deposit enough 40$";
	redirect::location("../orderhistory.php");
	unset($_SESSION["cc"]);
	unset($_SESSION["ccid"]);
	unset($_SESSION["moneyplus"]);

}
else 
{
if($Plan=="ourplan")
{
	$MoneyPlus=$_SESSION["moneyplus"];
	$TotalPlus=count($_SESSION["ccid"])*$MoneyPlus;
	$CcIdList=$_SESSION["ccid"];
	$Condition=database::BuildCondition(array(
	'ccid' => $CcIdList
	)
	);
	$MoneyPriceTemp=database::LimitQuery('ccinfo',array(
	'select' => 'sum(price)',
	'condition' => $Condition
	));
	$time=date("Y-m-d-H-i-s");
	$MoneyPrice=$MoneyPriceTemp[0]['sum(price)']+$TotalPlus;
	$MoneyComplete=round($MoneyPrice,2);
	$CheckMoney=user::checkpermission('userinfo',"userid=$UserId", "money >= $MoneyComplete");
	if($CheckMoney==1)
	{
		$UpdateMoney=database::Update('userinfo',array(
		'userid' => $UserId
		),array('money' => array( "money - {$MoneyComplete}" ))
		);
		$InsertOrder=user::insertorder($UserId, $MoneyComplete, 'seller');
		foreach($_SESSION["cc"] as $GetCcId)
		{
			$CcId=$GetCcId["ccid"];
			$GetPrice=database::LimitQuery('ccinfo',array(
			'select' => 'price',
			'condition' => array(
			'ccid' => $CcId
			)
			));
			$Price=$GetPrice[0]["price"];
			$PriceSell=$Price+$MoneyPlus;
			$UpdateCc=database::Update('ccinfo',array(
			'ccid' => $CcId
			),array(
			'pricesell' => $PriceSell,
			'orderid' => $InsertOrder,
			'userid' => $UserId,
			'timeorder' => $time
			
			));
			
		}
		$_SESSION["erro"]="Thank You, Your order has been completed";
		redirect::location("../orderhistory.php");
		
	}
	else
	{
		$_SESSION["erro"]="You Do not have enough money to buy, you need $MoneyComplete to buy these cc";
		unset($_SESSION["cc"]);
		redirect::location("../lr/index.php");
	}
	
}
}
?>